<?php
namespace Common\Extend;
/**
 * 1.第三方回复加密消息给公众平台；
 * 2.第三方收到公众平台发送的消息，验证消息的安全性，并对消息进行解密。
 */
class wxTsMsg
{
    private $appid;
    private $appsecret;
    /**
     * 构造函数
     * @param $appid string 开放平台appid
     * @param $appsecret string 开放平台appsecret
     */
    public function __construct(){
        $this->appid = 'wxd4941f0c17f04be4'; //开放平台appid
        $this->appsecret = 'a2a575f41a1b26a0aa9d769259eb3275';//开放平台appsecret
    }

    //获取第三方平台component_access_token
    public function get_component_access_token(){
        $component_verify_ticket = M('wx_encrypt_msg')->field('encryptMsg')->find();
        $data=[
            'component_appid'=>$this->appid,
            'component_appsecret'=>$this->appsecret,
            'component_verify_ticket'=>$component_verify_ticket['encryptmsg'],
        ];
        //var_dump($data);
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token';
        $res = https_request($url,$data,'json');
        $token= json_decode($res,true);
        return $token['component_access_token'];
    }
    //获取预授权码pre_auth_code
    public function get_pre_auth_code($access_token){
        $data=[
            'component_appid'=>$this->appid,
        ];
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token='.$access_token;
        $res = https_request($url,$data,'json');
        $code = json_decode($res,true);
        //var_dump($code);exit;
        return $code['pre_auth_code'];
    }
    //使用授权码换取公众号或小程序的接口调用凭据和授权信息,获取授权token
    public function get_authorization($auth_code,$access_token){
        $data=[
            'component_appid'=>$this->appid,
            'authorization_code'=>'queryauthcode@@@cmHZVvqlQ4SFxMfA2FPgCu79JBkDqs4BWZMnneYAx7q5JGGnbdnsxNhwvu6n5B33_qMUZ4Wr5ehDJbKy-ZoZAg'
        ];
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth?component_access_token='.$access_token;
        $res = https_request($url,$data,'json');
        //var_dump($res);exit;
        $info = json_decode($res,true);
        return $info;
    }
    //获取（刷新）授权公众号或小程序的接口调用凭据（令牌）
    public function get_authorizer_token($auth_appid,$refresh_token,$access_token){
        $data=[
            'component_appid'=>$this->appid,
            'authorizer_appid'=>$auth_appid,
            'authorizer_refresh_token'=>$refresh_token
        ];
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token='.$access_token;
        $res = https_request($url,$data,'json');
        $token= json_decode($res,true);
        return $token['authorizer_access_token'];
    }
    //授权回调
    public function get_user_token(){
        $component_access_token = $this->get_component_access_token();
        $pre_auth_code = $this->get_pre_auth_code($component_access_token);
        $url = 'https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid='.$this->appid.'&pre_auth_code='.$pre_auth_code.'&redirect_uri=https://sj.258.com/';
        return $url;
    }
}

